//
//  HRNetworkingTool.swift
//  weiBo
//
//  Created by helong on 16/7/11.
//  Copyright © 2016年 helong. All rights reserved.
//

import UIKit
import AFNetworking
//定义请求的枚举,决定使用哪一种请求
enum RequestType: Int {
    case GET
    case POST
}

class HRNetworkingTool: AFHTTPSessionManager {

     // MARK: - 定义单例
    static let sharedSessionManager: HRNetworkingTool = {
        let manager = HRNetworkingTool()
        manager.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return manager
    }()
     // MARK: - 集成网络请求工具类
    /**
    使用的框架发生变化是修改此函数即可
    
    - parameter option:     请求的方式
    - parameter url:        请求的地址
    - parameter parameters: 请求的参数
    - parameter callBack:   请求成功或者失败的回调
    */
    func requestForResource(option: RequestType, url: String, parameters: AnyObject?,callBack:((response: AnyObject?, error: NSError?) -> Void)){
        if option == RequestType.GET{//get请求
            
            GET(url, parameters: parameters, progress: nil, success: { (_, response) -> Void in
                
                callBack(response: response,error: nil)
                
                }, failure: { (_, error) -> Void in
                    callBack(response: nil, error: error)
            })
        }
        if option == .POST{//POST请求
            POST(url, parameters: parameters, progress: nil, success: { (_, response) -> Void in
                callBack(response: response, error: nil)
                }, failure: { (_, error) -> Void in
                    callBack(response: nil, error: error)
            })
        }
    }
    
     // MARK: - 封装网络上传工具
    /**
    使用AFN封装上传图片的工具
    
    - parameter url:        地址
    - parameter parameters: 参数
    - parameter imageData:  要上传的图片的二进制数据
    - parameter fieldName:  服务器的字段名
    - parameter callBack:   回调
    */
    private func upload(url: String, parameters: AnyObject?, imageData: NSData, fieldName: String, callBack: (response: AnyObject?, error: NSError?) -> Void){
     
        POST(url, parameters: parameters, constructingBodyWithBlock: { (formData) -> Void in
            //  把图片转化成二进制手: 注意数据的类型,最好用万能类型
            formData.appendPartWithFileData(imageData, name: fieldName, fileName: "lala", mimeType: "octet-stream")
            
             formData.appendPartWithFileData(imageData, name: fieldName, fileName: "lala", mimeType: "octet-stream")
            
            }, progress: nil, success: { (_, response) -> Void in
                callBack(response: response, error: nil)
            }) { (_, error) -> Void in
                callBack(response: nil, error: error)
        }
 
    }
}
 // MARK: - 具体的网络请求
extension HRNetworkingTool{
    //  获取微博首页的数据
    func requestHomePageData(max_id: Int64 = 0, since_id:  Int64 = 0, callBack: (response: AnyObject?, error: NSError?) -> ()){
        //  准备参数
        let url = "https://api.weibo.com/2/statuses/friends_timeline.json"
        let params = [
            "access_token": HRUserAccountInfos.sharedUserAccount.access_token!,
            "max_id": "\(max_id)",
            "since_id":"\(since_id)"
        ]
        //  打印参数路径
        print(url + "?access_token=" + HRUserAccountInfos.sharedUserAccount.access_token!)
        //  使用通用方法完成请求
        requestForResource(.GET, url: url, parameters: params, callBack: callBack)
    }
    
    // MARK: - 网络请求获取access_token
    func requestAccessToken(code: String, callBack: (response: AnyObject?, error: NSError?) -> ()){
        //接口地址
        let url = "https://api.weibo.com/oauth2/access_token"
        //请求参数
        let parameters = [
            "client_id": appKey,
            "client_secret": appSecret,
            "grant_type": "authorization_code",
            "code": code,
            "redirect_uri": Redirect_Uri
        ]
        //  调用自定义的网络工具请求数据,并将闭包传递给自定义的网络工具,等他把结果回调到这里
        requestForResource(.POST, url: url, parameters: parameters, callBack: callBack)
   
        }
    
    
    
    
    //MARK: - 请求用户信息
func requestUserInfo(accountInfo: HRAccessToken, callBack:(response: AnyObject?, error: NSError?) -> Void){
        //  请求地址
        let url = "https://api.weibo.com/2/users/show.json" + "?access_token=\(accountInfo.access_token!)" + "&uid=\(accountInfo.uid)"
        
        let parameters = [
            "access_token": accountInfo.access_token!,
            "uid": "\(accountInfo.uid)"
        ]
    requestForResource(.GET, url: url, parameters: parameters, callBack: callBack)

}
    
     // MARK: - 请求微博首页的数据
    func requestData(callBack:(response: AnyObject?, error: NSError?) -> Void){
        
        let url = "https://api.weibo.com/2/statuses/friends_timeline.json"
        let params = [
            "access_token": HRUserAccountInfos.sharedUserAccount.access_token!
        ]
        requestForResource(.GET, url: url, parameters: params, callBack: callBack)
        
    }
    
     // MARK: - 发微博的请求
    func commitWeiBo(status: String, callBack: (response: AnyObject?, error: NSError?) -> Void) {
        
        let url = "https://api.weibo.com/2/statuses/update.json"
        
        let params = [
        "access_token": HRUserAccountInfos.sharedUserAccount.accountInfo!.access_token!,
        "status": status
        ]
        requestForResource(.POST, url: url, parameters: params, callBack: callBack)
        
    }
    
    //  发送带图片的微博的请求
    func commitWeiboWithPicture(status: String, formData: NSData, callBack: (response: AnyObject?, error: NSError?) -> Void) {
        let url = "https://upload.api.weibo.com/2/statuses/upload.json"
        let params = [
            "access_token": HRUserAccountInfos.sharedUserAccount.accountInfo!.access_token!,
            "status": status
        ]
        
        upload(url, parameters: params, imageData: formData, fieldName: "pic", callBack: callBack)
        
    }
}